package tests;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class TestingMail {

	public static void main(String[] args) 
	{
    	// Recipient's  and sender's email address respectively
      	String to = "karan.thaker@hotmail.co.uk";
      	String from = "totalitea.noreply@gmail.com";
	    
	    // GoogleMail smtp host
	    String host = "smtp.gmail.com";
	
	    // Get system properties
	    Properties properties = System.getProperties();
	
	    // Setup GoogleMail server
	    properties.setProperty("mail.smtp.host", host);			// GoogleMail smtp host
	    properties.put("mail.smtp.auth", "true");				// Authorisation is needed to send email
        properties.put("mail.smtp.starttls.enable", "true");	// TLS protocol is being used 
        properties.put("mail.smtp.port", "587"); 				// TLS Port
	
	    // Get the default Session object.
	    Session sesh = Session.getDefaultInstance(properties, 
	    		new javax.mail.Authenticator()
        		{ 
	    			protected PasswordAuthentication getPasswordAuthentication()
	    			{  											// Senders email			password
	    				return new PasswordAuthentication("totalitea.noreply@gmail.com", "totalitea123");
	    			}
        		});
        
    	Message message = new MimeMessage(sesh);
        try 
        {
			message.setFrom(new InternetAddress(from));
			message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));
		    message.setSubject("Thank you for your order from Totalitea!");
		    String msg="This is just an email to confirm that your order has been received.";
		    message.setContent(msg, "text/html; charset=utf-8");
		    Transport.send(message);
		} 
        catch (AddressException e) 
        {
        	e.printStackTrace();
		} 
        catch (MessagingException e) 
        {
			e.printStackTrace();
		}
	}

}
